﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.MODEL;
using KPIS.GERP.GHRMS.BLL;


namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class Login : System.Web.UI.Page
    {
        private UserBLL userBLL = new UserBLL();
        private EmployeeBLL empBLL = new EmployeeBLL();
        private SsMng ssMng  = new SsMng();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                CheckSingleSigon();
            }
        }

        private void CheckSingleSigon()
        {
            if (!String.IsNullOrEmpty(Request["username"]) && !String.IsNullOrEmpty(Request["password"]))
            {
                ProcessLogin(Request["username"], Request["password"]);
            }
        }

        protected void cmdLogin_Click(object sender, EventArgs e)
        {
            string strUserId = txtUserID.Text.ToUpper().Trim();
            string strPassword = txtPassword.Text.Trim();
	        ProcessLogin(strUserId, strPassword);
        }

		private void ProcessLogin(string strUserId, string strPassword)
		{
			if (!CheckPassword(strUserId, strPassword))
			{
				lblError.Text = "[รหัสผ่าน] ไม่ถูกต้อง";
				return;
			}
			Response.Redirect(ServerName + @"App_UI/Form/Default.aspx");
		}

        private bool CheckPassword(string strUserId, string strPassword)
        {
            bool bOk = false;

            DataRowCollection drc = userBLL.Login(strUserId, strPassword).Tables[0].Rows;

            if (drc.Count > 0)
            {
                string userStatus = drc[0]["user_status"].ToString();

                if (userStatus == SystemInfo._RejectStatus)
                {
                    //RadManager1.Alert("รอการตรวจสอบจากผู้ดูแลระบบ");
                }
                else if (userStatus == "A")
                {
                    ssMng.SetLoginSession(drc[0]["user_seq"].ToString(),
						drc[0]["emp_seq"].ToString(),
						drc[0]["user_name"].ToString(),
						LoadEmployees(drc[0]["emp_seq"].ToString()),
                        String.IsNullOrEmpty(hHeight.Value) ? "600" : hHeight.Value);
                    bOk = true;
                }
            }
            return bOk;
        }

        public string LoadEmployees(string empSeq=null)
        {
            empSeq = empSeq ?? ssMng.EmpSeq;
			string workingJobSiteSeq = empBLL.GetWorkingJobSiteByEmpSeq(empSeq);
	        return workingJobSiteSeq;
        }

        public string ServerName
        {
            get
            {
                string serverProtocal = Request.ServerVariables["SERVER_PROTOCOL"].ToLower().StartsWith("https") ? "https" : "http";
                string serverHost = Request.Url.Host;
                string serverPort = Request.Url.Port == 80 ? String.Empty : ":" + Request.Url.Port;

                return String.Format("{0}://{1}{2}/", serverProtocal, serverHost, serverPort);
            }
        }
    }
}
